import axios from "axios"
import { Notification } from '@arco-design/web-vue';

const BaseUrl = "/api"
const get = (url,params)=>{
    const http = axios.create({
        baseURL:BaseUrl,
        withCredentials:true,
    })
    http.interceptors.request.use(setToken)
    http.interceptors.response.use(basic)
    return new Promise((resolve,reject)=>{
            http.get(url,{params}).then(res => resolve(res)).catch(res=>reject(res))
    })
}
const post = (url,params)=>{
    const http = axios.create({
        baseURL:BaseUrl,
        withCredentials:true,
    })
    http.interceptors.request.use(setToken)
    http.interceptors.response.use(basic)
    return new Promise((resolve,reject)=>{
        http.post(url,params).then(res => resolve(res)).catch(res=>reject(res))
    })
}
const basic = (response)=>{
    if (response.data.code == 101) {
        Notification.error({
            title: '错误提示',
            content: "登录过期，请刷新页面后登录！",
        })
        localStorage.removeItem("token")
        location.reload()
    }else{
        return response
    }
    
}
const setToken = (config)=>{
    config.headers.AuthBasic = localStorage.getItem("token")
    return config
}
// post.interceptors.AuthBasic.request.use(setToken)
// get.interceptors.AuthBasic.request.use(setToken)
export {get,post}